using System;
using System.Data;
using System.Data.SqlClient;

namespace Company.BLL.KDT.GC
{
	public partial class HangChuyenTiep : EntityBase
	{
		#region Private members.
		
		protected long _ID;
		protected long _Master_ID;
		protected int _SoThuTuHang;
		protected string _MaHS = String.Empty;
		protected string _MaHang = String.Empty;
		protected string _TenHang = String.Empty;
        protected string _ID_NuocXX = String.Empty;
        protected string _ID_DVT = String.Empty;
        protected decimal _SoLuong;
		protected decimal _DonGia;
		protected decimal _TriGia;
		
		#endregion
		
		//---------------------------------------------------------------------------------------------

		#region Properties.
		
		public long ID
		{
			set {this._ID = value;}
			get {return this._ID;}
		}
        public long Master_ID
		{
			set {this._Master_ID = value;}
            get { return this._Master_ID; }
		}
		public int SoThuTuHang
		{
			set {this._SoThuTuHang = value;}
			get {return this._SoThuTuHang;}
		}
		public string MaHS
		{
			set {this._MaHS = value;}
			get {return this._MaHS;}
		}
		public string MaHang
		{
			set {this._MaHang = value;}
            get { return this._MaHang; }
		}
		public string TenHang
		{
			set {this._TenHang = value;}
			get {return this._TenHang;}
		}
        public string ID_NuocXX
		{
			set {this._ID_NuocXX = value;}
            get { return this._ID_NuocXX; }
		}
		public string ID_DVT
		{
			set {this._ID_DVT = value;}
            get { return this._ID_DVT; }
		}
		public decimal SoLuong
		{
			set {this._SoLuong = value;}
			get {return this._SoLuong;}
		}

		public decimal DonGia
		{
			set {this._DonGia = value;}
			get {return this._DonGia;}
		}
		public decimal TriGia
		{
			set {this._TriGia = value;}
			get {return this._TriGia;}
		}
		
		//---------------------------------------------------------------------------------------------
        
		public bool IsExist
        {
            get 
            { 
                return this.Load();  
            }
        }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public bool Load()
		{
			string spName = "p_KDT_GC_HangChuyenTiep_Load";
            SqlCommand dbCommand = (SqlCommand) this.db.GetStoredProcCommand(spName);
			
			this.db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
			
            IDataReader reader = this.db.ExecuteReader(dbCommand);
			if (reader.Read())
			{
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) this._ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) this._Master_ID = reader.GetInt64(reader.GetOrdinal("Master_ID"));
				//if (!reader.IsDBNull(reader.GetOrdinal("SoThuTuHang"))) this._SoThuTuHang = reader.GetInt32(reader.GetOrdinal("SoThuTuHang"));
				if (!reader.IsDBNull(reader.GetOrdinal("MaHS"))) this._MaHS = reader.GetString(reader.GetOrdinal("MaHS"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) this._MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
				if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) this._TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_NuocXX"))) this._ID_NuocXX = reader.GetString(reader.GetOrdinal("ID_NuocXX"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_DVT"))) this._ID_DVT = reader.GetString(reader.GetOrdinal("ID_DVT"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) this._SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
				if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) this._DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
				if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) this._TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
                reader.Close();
				return true;
			}
			return false;
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public HangChuyenTiepCollection SelectCollectionBy_Master_ID()
		{
            string spName = "p_KDT_GC_HangChuyenTiep_SelectBy_Master_ID";
            SqlCommand dbCommand = (SqlCommand) this.db.GetStoredProcCommand(spName);

            this.db.AddInParameter(dbCommand, "@Master_ID", SqlDbType.BigInt, this._Master_ID);

            HangChuyenTiepCollection collection = new HangChuyenTiepCollection();
            IDataReader reader = this.db.ExecuteReader(dbCommand);
			while (reader.Read())
			{
                HangChuyenTiep entity = new HangChuyenTiep();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity._ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) entity._Master_ID = reader.GetInt64(reader.GetOrdinal("Master_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoThuTuHang"))) entity._SoThuTuHang = reader.GetInt32(reader.GetOrdinal("SoThuTuHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHS"))) entity._MaHS = reader.GetString(reader.GetOrdinal("MaHS"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) entity._MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) entity._TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_NuocXX"))) entity._ID_NuocXX = reader.GetString(reader.GetOrdinal("ID_NuocXX"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_DVT"))) entity._ID_DVT = reader.GetString(reader.GetOrdinal("ID_DVT"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) entity._SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) entity._DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) entity._TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
				collection.Add(entity);
			}
            reader.Close();
			return collection;
		}

		//---------------------------------------------------------------------------------------------

		public DataSet SelectBy_Master_ID()
		{
            string spName = "p_KDT_GC_HangChuyenTiep_SelectBy_Master_ID";
            SqlCommand dbCommand = (SqlCommand) this.db.GetStoredProcCommand(spName);

            this.db.AddInParameter(dbCommand, "@Master_ID", SqlDbType.BigInt, this._Master_ID);
						
            return this.db.ExecuteDataSet(dbCommand);
		}
		//---------------------------------------------------------------------------------------------

		public DataSet SelectAll()
        {
            string spName = "p_KDT_GC_HangChuyenTiep_SelectAll";
            SqlCommand dbCommand = (SqlCommand) this.db.GetStoredProcCommand(spName);
            return this.db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------

		public IDataReader SelectReaderAll()
        {
            string spName = "p_KDT_GC_HangChuyenTiep_SelectAll";
            SqlCommand dbCommand = (SqlCommand) this.db.GetStoredProcCommand(spName);
            return this.db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            string spName = "p_KDT_GC_HangChuyenTiep_SelectDynamic";
            SqlCommand dbCommand = (SqlCommand) this.db.GetStoredProcCommand(spName);

			this.db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            this.db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return this.db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------

		public IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            string spName = "p_KDT_GC_HangChuyenTiep_SelectDynamic";
            SqlCommand dbCommand = (SqlCommand) this.db.GetStoredProcCommand(spName);

			this.db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            this.db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return this.db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		public HangChuyenTiepCollection SelectCollectionAll()
		{
			HangChuyenTiepCollection collection = new HangChuyenTiepCollection();

			IDataReader reader = this.SelectReaderAll();
			while (reader.Read())
			{
				HangChuyenTiep entity = new HangChuyenTiep();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity._ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) entity._Master_ID = reader.GetInt64(reader.GetOrdinal("Master_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoThuTuHang"))) entity._SoThuTuHang = reader.GetInt32(reader.GetOrdinal("SoThuTuHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHS"))) entity._MaHS = reader.GetString(reader.GetOrdinal("MaHS"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) entity._MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) entity._TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_NuocXX"))) entity._ID_NuocXX = reader.GetString(reader.GetOrdinal("ID_NuocXX"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_DVT"))) entity._ID_DVT = reader.GetString(reader.GetOrdinal("ID_DVT"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) entity._SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) entity._DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) entity._TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
				collection.Add(entity);
			}
			return collection;			
		}
		
		//---------------------------------------------------------------------------------------------
		
		public HangChuyenTiepCollection SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			HangChuyenTiepCollection collection = new HangChuyenTiepCollection();

			IDataReader reader = this.SelectReaderDynamic(whereCondition, orderByExpression);
			while (reader.Read())
			{
				HangChuyenTiep entity = new HangChuyenTiep();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity._ID = reader.GetInt64(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Master_ID"))) entity._Master_ID = reader.GetInt64(reader.GetOrdinal("Master_ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoThuTuHang"))) entity._SoThuTuHang = reader.GetInt32(reader.GetOrdinal("SoThuTuHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHS"))) entity._MaHS = reader.GetString(reader.GetOrdinal("MaHS"));
                if (!reader.IsDBNull(reader.GetOrdinal("MaHang"))) entity._MaHang = reader.GetString(reader.GetOrdinal("MaHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("TenHang"))) entity._TenHang = reader.GetString(reader.GetOrdinal("TenHang"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_NuocXX"))) entity._ID_NuocXX = reader.GetString(reader.GetOrdinal("ID_NuocXX"));
                if (!reader.IsDBNull(reader.GetOrdinal("ID_DVT"))) entity._ID_DVT = reader.GetString(reader.GetOrdinal("ID_DVT"));
                if (!reader.IsDBNull(reader.GetOrdinal("SoLuong"))) entity._SoLuong = reader.GetDecimal(reader.GetOrdinal("SoLuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonGia"))) entity._DonGia = reader.GetDecimal(reader.GetOrdinal("DonGia"));
                if (!reader.IsDBNull(reader.GetOrdinal("TriGia"))) entity._TriGia = reader.GetDecimal(reader.GetOrdinal("TriGia"));
				collection.Add(entity);
			}
			return collection;			
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public long Insert()
		{
			return this.InsertTransaction(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public long InsertTransaction(SqlTransaction transaction)
		{			
			string spName = "p_KDT_GC_HangChuyenTiep_Insert";		
			SqlCommand dbCommand = (SqlCommand) this.db.GetStoredProcCommand(spName);
			this.db.AddOutParameter(dbCommand, "@ID", SqlDbType.BigInt, 8);
			this.db.AddInParameter(dbCommand, "@Master_ID", SqlDbType.BigInt, this._Master_ID);
			this.db.AddInParameter(dbCommand, "@SoThuTuHang", SqlDbType.Int, this._SoThuTuHang);
			this.db.AddInParameter(dbCommand, "@MaHS", SqlDbType.VarChar, this._MaHS);
			this.db.AddInParameter(dbCommand, "@MaHang", SqlDbType.VarChar, this._MaHang);
			this.db.AddInParameter(dbCommand, "@TenHang", SqlDbType.NVarChar, this._TenHang);
			this.db.AddInParameter(dbCommand, "@ID_NuocXX", SqlDbType.Char, this._ID_NuocXX);
			this.db.AddInParameter(dbCommand, "@ID_DVT", SqlDbType.Char, this._ID_DVT);
			this.db.AddInParameter(dbCommand, "@SoLuong", SqlDbType.Decimal, this._SoLuong);
			this.db.AddInParameter(dbCommand, "@DonGia", SqlDbType.Money, this._DonGia);
			this.db.AddInParameter(dbCommand, "@TriGia", SqlDbType.Money, this._TriGia);	
			if (transaction != null)
			{
				this.db.ExecuteNonQuery(dbCommand, transaction);
				this._ID = (long) this.db.GetParameterValue(dbCommand, "@ID");
				return this._ID;
			}
            else
			{
				this.db.ExecuteNonQuery(dbCommand);
				this._ID = (long) this.db.GetParameterValue(dbCommand, "@ID");
				return this._ID;
			}			
		}
		
		//---------------------------------------------------------------------------------------------
		
		public bool Insert(HangChuyenTiepCollection collection)
        {
            bool ret;
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (HangChuyenTiep item in collection)
                    {
                        if (item.InsertTransaction(transaction) <= 0)
						{							
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
                    	transaction.Commit();
                    	ret = true;
					}
					else
					{
                    	transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}
		
		//---------------------------------------------------------------------------------------------		
        
		public void InsertTransaction(SqlTransaction transaction, HangChuyenTiepCollection collection)
        {
            foreach (HangChuyenTiep item in collection)
            {
               	item.InsertTransaction(transaction);
            }
        }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		public int InsertUpdate()
		{
			return this.InsertUpdateTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdateTransaction(SqlTransaction transaction)
		{			
			string spName = "p_KDT_GC_HangChuyenTiep_InsertUpdate";		
			SqlCommand dbCommand = (SqlCommand) this.db.GetStoredProcCommand(spName);
            this.db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
            this.db.AddInParameter(dbCommand, "@Master_ID", SqlDbType.BigInt, this._Master_ID);
            this.db.AddInParameter(dbCommand, "@SoThuTuHang", SqlDbType.Int, this._SoThuTuHang);
            this.db.AddInParameter(dbCommand, "@MaHS", SqlDbType.VarChar, this._MaHS);
            this.db.AddInParameter(dbCommand, "@MaHang", SqlDbType.VarChar, this._MaHang);
            this.db.AddInParameter(dbCommand, "@TenHang", SqlDbType.NVarChar, this._TenHang);
            this.db.AddInParameter(dbCommand, "@ID_NuocXX", SqlDbType.Char, this._ID_NuocXX);
            this.db.AddInParameter(dbCommand, "@ID_DVT", SqlDbType.Char, this._ID_DVT);
            this.db.AddInParameter(dbCommand, "@SoLuong", SqlDbType.Decimal, this._SoLuong);
            this.db.AddInParameter(dbCommand, "@DonGia", SqlDbType.Money, this._DonGia);
            this.db.AddInParameter(dbCommand, "@TriGia", SqlDbType.Money, this._TriGia);	
			if (transaction != null)
                return this.db.ExecuteNonQuery(dbCommand, transaction);
            else
                return this.db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		
		public bool InsertUpdate(HangChuyenTiepCollection collection)
        {
            bool ret;
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (HangChuyenTiep item in collection)
                    {
                        if (item.InsertUpdateTransaction(transaction) <= 0)
						{
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
						transaction.Commit();
						ret = true;
					}
					else
					{
						transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}		
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public int Update()
		{
			return this.UpdateTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int UpdateTransaction(SqlTransaction transaction)
		{
			string spName = "p_KDT_GC_HangChuyenTiep_Update";		
			SqlCommand dbCommand = (SqlCommand) this.db.GetStoredProcCommand(spName);

            this.db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
            this.db.AddInParameter(dbCommand, "@Master_ID", SqlDbType.BigInt, this._Master_ID);
            this.db.AddInParameter(dbCommand, "@SoThuTuHang", SqlDbType.Int, this._SoThuTuHang);
            this.db.AddInParameter(dbCommand, "@MaHS", SqlDbType.VarChar, this._MaHS);
            this.db.AddInParameter(dbCommand, "@MaHang", SqlDbType.VarChar, this._MaHang);
            this.db.AddInParameter(dbCommand, "@TenHang", SqlDbType.NVarChar, this._TenHang);
            this.db.AddInParameter(dbCommand, "@ID_NuocXX", SqlDbType.Char, this._ID_NuocXX);
            this.db.AddInParameter(dbCommand, "@ID_DVT", SqlDbType.Char, this._ID_DVT);
            this.db.AddInParameter(dbCommand, "@SoLuong", SqlDbType.Decimal, this._SoLuong);
            this.db.AddInParameter(dbCommand, "@DonGia", SqlDbType.Money, this._DonGia);
            this.db.AddInParameter(dbCommand, "@TriGia", SqlDbType.Money, this._TriGia);	
			if (transaction != null)
                return this.db.ExecuteNonQuery(dbCommand, transaction);
            else
                return this.db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public void UpdateCollection(HangChuyenTiepCollection collection, SqlTransaction transaction)
        {
            foreach (HangChuyenTiep item in collection)
            {
                item.UpdateTransaction(transaction);
            }
        }

		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public int Delete()
		{
			return this.DeleteTransaction(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int DeleteTransaction(SqlTransaction transaction)
		{
			string spName = "p_KDT_GC_HangChuyenTiep_Delete";		
			SqlCommand dbCommand = (SqlCommand) this.db.GetStoredProcCommand(spName);
			
			this.db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, this._ID);
			
			if (transaction != null)
                return this.db.ExecuteNonQuery(dbCommand, transaction);
            else
                return this.db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
        public void DeleteCollection(HangChuyenTiepCollection collection, SqlTransaction transaction)
        {
            foreach (HangChuyenTiep item in collection)
            {
                item.DeleteTransaction(transaction);
            }
        }

		//---------------------------------------------------------------------------------------------
		
		public bool DeleteCollection(HangChuyenTiepCollection collection)
        {
            bool ret;
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
					bool ret01 = true;
                    foreach (HangChuyenTiep item in collection)
                    {
                        if (item.DeleteTransaction(transaction) <= 0)
						{
							ret01 = false;
							break;
						}
                    }
					if (ret01)
					{
						transaction.Commit();
						ret = true;
					}
					else
					{
						transaction.Rollback();
						ret = false;                    	
					}
                }
                catch
                {
                    ret = false;
                    transaction.Rollback();
                }
                finally 
                {
                    connection.Close();
                }
            }
            return ret;		
		}
		#endregion
	}	
}